home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Libraries / usr (gcc 1.37 libs) / mac / write.c < prev   
C/C++ Source or Header  |  1993-12-14  |  1KB  |  56 lines

  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <fcntl.h>
  4. #include "crtlocal.h"
  5.  
  6. ssize_t write(int fd, const void *buf, size_t size)
  7.     {
  8.     OSErr err;
  9.     IOParam pb;
  10.     if (!crt_fd_tab[fd].fd) return cwrite(fd,buf,size);
  11.     else if (crt_fd_tab[fd].flags & O_PIPE)
  12.         {
  13.         return writepipe(fd, (char *)buf, size);
  14.         }
  15.     else
  16.         {
  17.         pb.ioCompletion = 0;
  18.         pb.ioRefNum = crt_fd_tab[fd].fd;
  19.         pb.ioReqCount = size;
  20.         pb.ioPosMode = fsAtMark;
  21.         pb.ioBuffer = (void *)buf;
  22.         if (crt_fd_tab[fd].flags & O_TEXT)
  23.             {
  24.             void *tmpmem = NewPtr(size);
  25.             char *newp = pb.ioBuffer = tmpmem;
  26.             char *oldbuf = (char *)buf;
  27.             while (size--)
  28.                 {
  29.                 if (*oldbuf == '\r') 
  30.                     {
  31.                     *newp++ = '\n';
  32.                     oldbuf++;
  33.                     }
  34.                 else *newp++ = *oldbuf++;
  35.                 }
  36.             PBWriteSync((ParmBlkPtr)&pb);
  37.             DisposPtr(tmpmem);
  38.             }
  39.         else PBWriteSync((ParmBlkPtr)&pb);
  40.         err = pb.ioResult;
  41.         mysleep(1);
  42.         return pb.ioActCount;
  43.         }
  44.     }
  45.  
  46. #include <sys/uio.h>
  47.  
  48. int writev(int fd, const struct iovec *iov, int iovcnt)
  49.     {
  50.     int i;
  51.     for (i = 0; i < iovcnt; i++)
  52.         {
  53.         write(fd, iov[i].iov_base, iov[i].iov_len);
  54.         }
  55.     }
  56.